package cn.kgc.controller;

import cn.kgc.annotation.LoginType;
import cn.kgc.annotation.RequireLogin;
import cn.kgc.base.SystemConstant;
import cn.kgc.entity.ReportJobBo;
import cn.kgc.entity.ReportJobMO;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.service.ReportJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author YC
 */
@RestController
@RequestMapping("/report")
@Api(tags = "举报管理模块")
public class ReportJobController {

    @Resource
    private ReportJobService reportJobService;

    @PostMapping("/save")
    @ApiOperation(value = "举报职位")
    @RequireLogin(value = LoginType.USER_APP_LOGIN)
    public GraceJSONResult reportJob(@RequestBody ReportJobMO reportJobMo) {
        // 判断职位是否存在
        String jobId = reportJobMo.getJobId();
        if (reportJobService.getByJobId(jobId) == null) {
            return GraceJSONResult.errorMsg("职位已经被举报了");
        }
        reportJobService.addReportJob(reportJobMo);
        return GraceJSONResult.ok();
    }

    @PostMapping("/page")
    @ApiOperation(value = "举报职位")
    @RequireLogin(value = LoginType.USER_ADMIN_LOGIN)
    public GraceJSONResult page(@RequestBody ReportJobBo reportJobBo) {
        return GraceJSONResult.ok(reportJobService.page(reportJobBo));
    }

    @PostMapping("/dealSuccess")
    @ApiOperation(value = "处理举报通过")
    @RequireLogin(value = LoginType.USER_ADMIN_LOGIN)
    public GraceJSONResult dealReportJobSuccess(String jobId) {
        ReportJobMO reportJobMo = reportJobService.getByJobId(jobId);
        if (reportJobMo.getDealStaus() != SystemConstant.REPORT_JOB_UNTREATED) {
            return GraceJSONResult.errorMsg("此职位的举报已经被处理");
        }
        reportJobService.dealReportJobSuccess(jobId, SystemConstant.REPORT_JOB_SUCCESS);
        return GraceJSONResult.ok();
    }

    @PostMapping("/dealFail")
    @ApiOperation(value = "处理举报未通过")
    @RequireLogin(value = LoginType.USER_ADMIN_LOGIN)
    public GraceJSONResult dealReportJobFail(String jobId) {
        ReportJobMO reportJobMo = reportJobService.getByJobId(jobId);
        if (reportJobMo.getDealStaus() != SystemConstant.REPORT_JOB_UNTREATED) {
            return GraceJSONResult.errorMsg("此职位的举报已经被处理");
        }
        reportJobService.dealReportJobSuccess(jobId, SystemConstant.REPORT_JOB_FAIL);
        return GraceJSONResult.ok();
    }

}